// const MD5 = require('md5');
const _ = require('lodash');

let getUrlArgs = function () {
  let args = {};
  let hash = window.location.hash;//打包后访问存在的 #/ 属于hash部分，需要处理
  let query = window.location.search.substring(1) || hash.substring(hash.indexOf('?') + 1);
  let pairs = query.split('&');
  for (let i in pairs) {
    let pos = pairs[i].indexOf('=');
    if (pos === -1) { continue };
    let key = pairs[i].substring(0, pos);
    let value = decodeURIComponent(pairs[i].substring(pos + 1));
    args[key] = value;
  }
  return args;
};
let getLS = function (key) {
  let res = window.localStorage.getItem(key);
  try {
    return JSON.parse(res);
  } catch (err) {
    console.error(`localStorage检测到意义不明的字符串${res}`, err)
    return res
  }
};
let setLS = function (key, value) {
  let val = _.isString(value) ? value : JSON.stringify(value);
  window.localStorage.setItem(key, val);
  return
};
// removeLS: function (key) {
//   return window.localStorage.removeItem(key);
// },
// clearLS: function () {
//   return window.localStorage.clear();
// },
// 上面是localStorage的部分，考虑常用的就get/set部分，后面的部分这次就不予封装了
let getSS = function (key) {
  let res = window.sessionStorage.getItem(key);
  try {
    return JSON.parse(res);
  } catch (err) {
    console.error(`sessionStorage检测到意义不明的字符串${res}`, err)
    return res
  }
};
let setSS = function (key, value) {
  let val = _.isString(value) ? value : JSON.stringify(value);
  window.sessionStorage.setItem(key, val);
  return
};

let storage = { getUrlArgs, getLS, setLS, getSS, setSS };
export default storage;
